<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/12/18
 * Time: 11:08
 */

namespace app\admin\controller;

use think\Db;

class Member extends Base
{
    /**
     * 用户列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $where['user_status'] = ['BETWEEN', [1, 2]];
            $keyword = trim(input('keyword'), ' '); // 用户ID/手机号/姓名/昵称
            $start_time = input('start_time'); // 注册开始时间
            $end_time = input('end_time'); // 注册结束时间
            $login_start_time = input('login_start_time'); // 登录开始时间
            $login_end_time = input('login_end_time'); // 登录结束时间

            // 关键字
            if(isset($keyword) && $keyword != ''){
                $where['user_id|user_phone|user_username'] = ['like', '%' . $keyword . '%'];
            }
            // 注册时间
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['user_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['user_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['user_time'] = ['<=', $end_time];
            }
            // 登录时间
            if(isset($login_start_time) && $login_start_time != '' && isset($login_end_time) && $login_end_time != ''){
                $where['user_last_login'] = ['between', [$login_start_time, $login_end_time]];
            }
            if(isset($login_start_time) && $login_start_time != '' && isset($login_end_time) && $login_end_time == ''){
                $where['user_last_login'] = ['>=', $login_start_time];
            }
            if(isset($login_start_time) && $login_start_time == '' && isset($login_end_time) && $login_end_time != ''){
                $where['user_last_login'] = ['<=', $login_end_time];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')->where($where)->count();// 获取总条数
            $lists = Db::name('u_user')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('user_status asc, user_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 用户状态
     * @return \think\response\Json
     */
    public function userStatus(){
        $user_id = input('id');
        $status = Db::table('up_u_user')->where('user_id', $user_id)->value('user_status');
        $newStatus = $status == 1 ? 2 : 1;
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', $newStatus);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 删除用户
     * @return \think\response\Json
     */
    public function userDel(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}